package com.harvey.guava.concurrent;

import com.google.common.util.concurrent.RateLimiter;
import com.sun.corba.se.impl.resolver.SplitLocalResolverImpl;

import javax.swing.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.stream.IntStream;

/**
 * @author harvey
 * @Description
 * @date 2020-03-30 10:57
 * @since
 */

public class RateLimiterExample {

    public static void main(String[] args) {

        ExecutorService executorService = Executors.newFixedThreadPool(10);



        IntStream.range(0, 10).forEach( num ->
                executorService.submit(RateLimiterExample::testLimiter)
        );


    }

    /**
     * 0.5，就是说，一秒钟只能有0.5次的操作。
     * 换言之，2秒钟有一次操作
     */
    private final static RateLimiter rateLimiter = RateLimiter.create(0.5);

    public static void testLimiter() {
        System.out.println(Thread.currentThread().getName() + " -> acquired:" + rateLimiter.acquire());
    }

}
